package com.zyk.leetcode;

/**
 * @author zhangsan
 * @date 2021/5/5 10:46
 */
public class C443 {

    public static int compress(char[] chars) {
        char pre = chars[0];
        int cnt = 1, ans = 0, index = 1;
        for (int i = 1; i < chars.length; i++) {
            if (chars[i] == pre) {
                cnt++;
            } else {
                if (cnt == 1) {
                    ans += 1;
                } else {
                    char[] tmp = String.valueOf(cnt).toCharArray();
                    ans += tmp.length + 1;
                    for (char c : tmp) {
                        chars[index++] = c;
                    }
                }
                pre = chars[i];
                cnt = 1;
                chars[index++] = pre;
            }
        }
        if (cnt == 1) {
            ans += 1;
        } else {
            char[] tmp = String.valueOf(cnt).toCharArray();
            ans += tmp.length + 1;
            for (char c : tmp) {
                chars[index++] = c;
            }
        }
        return ans;
    }


    // for test
    public static void main(String[] args) {
        char[] str = {'a', 'a', 'b', 'b', 'c', 'c', 'c'};
        System.out.println(compress(str));
        System.out.println(new String(str));
    }

}
